/*
 * Copyright (C) 2010 Graham Allan
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.strath.cis.grallan.findbugs.adaptiveranking.population;

import edu.strath.cis.grallan.findbugs.adaptiveranking.UserDesignationToPositivesMap;
import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.ClassAnnotation;
import edu.umd.cs.findbugs.MethodAnnotation;

/**
 * @author Graham Allan (grallan@cis.strath.ac.uk)
 */
public class MethodPopulation extends AbstractPopulation {

	private final MethodAnnotation methodForPopulation;

	private static class NullMethodPopulation extends MethodPopulation implements NullPopulation {
		public NullMethodPopulation() {
			super(new MethodAnnotation("NullMethodAnnotation", "NullMethodAnnotation", "()V;", false),
					designationMapper());
		}

		@Override
		public Population addAlert(BugInstance bugAlert) {
			// No-op
			return this;
		}

		@Override
		public double adjustmentFactor() {
			return 0.0d;
		}
	}

	public static final MethodPopulation NULL_METHOD_POPULATION = new NullMethodPopulation();

	private static UserDesignationToPositivesMap designationMapper() {
		return new UserDesignationToPositivesMap();
	}

	public static MethodPopulation populationOf(MethodAnnotation methodOfAlert) {
		return new MethodPopulation(methodOfAlert, designationMapper());
	}

	/**
	 * @param methodForPopulation
	 * @see #populationOf()
	 */
	private MethodPopulation(MethodAnnotation methodForPopulation, UserDesignationToPositivesMap designationMapper) {
		super(designationMapper);
		if (methodForPopulation == null)
			throw new PopulationException(new NullPointerException("Method cannot be null for a MethodPopulation."));
		this.methodForPopulation = methodForPopulation;
	}

	@Override
	protected void checkAlert(BugInstance bugAlert) throws NullPointerException, PopulationException {
		super.checkAlert(bugAlert);

		MethodAnnotation methodOfAlert = bugAlert.getPrimaryMethod();

		if (methodOfAlert == null)
			throw new PopulationException(
					new NullPointerException(
							"An alert added to a MethodPopulation must return a non-null MethodAnnotation from getPrimaryMethod()"));

		if (methodForPopulation.compareTo(methodOfAlert) != 0)
			throw new PopulationException("Cannot add an alert from a different method to this MethodPopulation");

	}

	public String populationDescriptor() {
		ClassAnnotation primaryClass = new ClassAnnotation(methodForPopulation.getClassName());
		return methodForPopulation.getFullMethod(primaryClass);
	}

}
